package com.ebraga.trackerclub.android.dao.core;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;

import com.ebraga.trackerclub.android.dao.api.ParameterDao;
import com.ebraga.trackerclub.android.dao.entity.ParameterAndroid;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.dao.BackofficeDaoException;
import com.ebraga.trackerclub.android.exception.dao.BackofficeIllegalArgumentDaoException;
import com.ebraga.trackerclub.android.util.api.AndroidContentValues;
import com.ebraga.trackerclub.android.util.api.AndroidSQLiteDatabase;
import com.ebraga.trackerclub.android.util.api.DataAccessObject;

public abstract class ParameterDaoImpl implements ParameterDao {
	
	private DataAccessObject dataAccessObject = null;
	private AndroidContentValues contentValues;

	protected ParameterDaoImpl(DataAccessObject dataAccessObject){
		this.dataAccessObject = dataAccessObject;
	}
	
	protected void setContentValues(AndroidContentValues contentValues) {
		this.contentValues = contentValues;
	}
	
	AndroidSQLiteDatabase sQLiteDatabase;
	Cursor c;
	String tableName = "parameter";
	String[] select = new String[] {XID_PARAMETER, NAME, VALUE, DESCRIPTION};
	
	// Column names
	public static final String XID_PARAMETER = "xidParameter";
	public static final String NAME          = "name";
	public static final String VALUE         = "value";
	public static final String DESCRIPTION   = "description";	

	public ParameterAndroid getParameter(String param)
			throws BackofficeDBConnectionException, BackofficeDaoException {	
			
		if (param == null){
			throw new BackofficeIllegalArgumentDaoException("Parametro vazio");
		}
			
		ParameterAndroid parameterAndroid = null;
		String where = NAME + " = ? ";
		String[] whereArgs = new String[] {param}; 
		try{
			sQLiteDatabase = dataAccessObject.getDB();
		}catch (BackofficeDaoException de){
			throw new BackofficeDBConnectionException("Erro ao conectar banco de dados");
		}
		
		try{
			c = sQLiteDatabase.query(tableName, select, where, whereArgs, null, null, null);
		}catch(Exception e){
			throw new BackofficeDaoException("Erro ao executar query");
		}
		
		List<ParameterAndroid> result = getRecordsFromCursor(c);
		
		if (result != null && result.size() > 0) {
			parameterAndroid =  result.get(0);
		}
		
		c.close();
		
		return parameterAndroid;
	}
	
	public List<ParameterAndroid> getParameters()
			throws BackofficeDBConnectionException, BackofficeDaoException {
				 
		try{
			sQLiteDatabase = dataAccessObject.getDB();
		}catch (BackofficeDaoException de){
			throw new BackofficeDBConnectionException("Erro ao conectar banco de dados");
		}
		
		try{
			c = sQLiteDatabase.query(tableName, select, null, null, null, null, null);
		}catch(Exception e){
			throw new BackofficeDaoException("Erro ao executar query");
		}
		
		List<ParameterAndroid> result = getRecordsFromCursor(c);		
		c.close();
		
		return result;
	}
	
	public void updateParameters(List<ParameterAndroid> parameterList)
			throws BackofficeDBConnectionException, BackofficeDaoException {
		
		if (parameterList == null){
			throw new BackofficeIllegalArgumentDaoException("Lista de parametros invalida");
		}
		
		try{
			sQLiteDatabase = dataAccessObject.getDB();
		}catch (BackofficeDaoException de){
			throw new BackofficeDBConnectionException("Erro ao conectar banco de dados", de);
		}
		
		try{
			sQLiteDatabase.beginTransaction();
			
			try{
				sQLiteDatabase.delete(tableName, null,null) ;
			}catch(Exception e){
				throw new BackofficeDaoException("Erro ao excluir parametros", e);
			}
			
			try{
				Long i = -1L;
				for (ParameterAndroid param : parameterList)	{	
					i = sQLiteDatabase.insert(tableName, createRecord(param));
				}
				
				if (i > -1L){
					sQLiteDatabase.setTransactionSuccessful();
				}
			}catch(Exception dao){
				throw new BackofficeDaoException("Erro ao inserir parametros", dao);
			}	
		}finally{
			sQLiteDatabase.endTransaction();
		}
				
	}
	
	public List<ParameterAndroid> getRecordsFromCursor(Cursor cursor) {
		
		List<ParameterAndroid> result = new ArrayList<ParameterAndroid>();
		
		while (cursor.moveToNext()) {
			
			ParameterAndroid parameterAndroid = new ParameterAndroid();
			parameterAndroid.setDescription(cursor.getString(cursor.getColumnIndexOrThrow(DESCRIPTION)));
			parameterAndroid.setName(cursor.getString(cursor.getColumnIndexOrThrow(NAME)));
			parameterAndroid.setValue(cursor.getString(cursor.getColumnIndexOrThrow(VALUE)));
			parameterAndroid.setXidParameter(cursor.getLong(cursor.getColumnIndexOrThrow(XID_PARAMETER)));

			result.add(parameterAndroid);
		}

		return result;
	}	
	
	
	public AndroidContentValues createRecord(ParameterAndroid parameterAndroid)	 {

		contentValues.put(XID_PARAMETER, parameterAndroid.getXidParameter());
		contentValues.put(DESCRIPTION, parameterAndroid.getDescription());
		contentValues.put(NAME, parameterAndroid.getName());
		contentValues.put(VALUE, parameterAndroid.getValue());

		return contentValues;
	}

}	
